﻿using System;
using System.Windows;
using System.Windows.Controls;
using Silvery;
using Silvery.Role.Simple;
using Silvery.Loader;

namespace Sample02_Role
{
    public class Sample_4 : GameRoot
    {
        public static Random random = new Random((int)DateTime.Now.Ticks);

        public Sample_4()
        {

            var role = new SimpleRole_ComplexRole(new Uri("download/Sample_Role/DownBody.zip", UriKind.Relative));
            this.AddBaseObj(role);
            role.Position = new Point(150, 100);

            Button addtest = new Button() {Content = "添加一个试试" };
            addtest.Click += new RoutedEventHandler(addtest_Click);
            this.Children.Add(addtest);
        }

        void addtest_Click(object sender, RoutedEventArgs e)
        {
            var t = new SimpleRole_ComplexRole(new Uri("download/Sample_Role/DownBody.zip", UriKind.Relative));
            this.AddBaseObj(t);
            t.Position = new Point(random.Next() % 500 + 40, random.Next() % 300 + 60);
        }
    }
    public class SimpleRole_ComplexRole : SimpleRole
    {
        ComboBox combobox = new ComboBox() { Width = 72 };
        UpperBody upperbody = new UpperBody();
        public SimpleRole_ComplexRole(Uri uri)
            : base(uri)
        {
            MouseEnterEffectOpened = true;
            RoleName = "我是一个复合角色";
            this.Children.Add(combobox);
            Canvas.SetLeft(combobox, -36);
            Canvas.SetTop(combobox, 40);
            combobox.SelectionChanged += new SelectionChangedEventHandler(combobox_SelectionChanged);
            var upperspirit = new SpiritLoader(new Uri("download/Sample_Role/UpperBody01.zip", UriKind.Relative), upperbody);
            upperspirit.BeginDownLoad();
            this.Parts.Add("UpperBody", upperbody);
            base.BeginDownload();
        }

        void combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.CurrentAnimatName = (string)combobox.SelectedItem;
        }
        protected override void OnBodyPackLoaded()
        {
            foreach (var item in BodyPart.SpiritData.AnimatNamesList)
            {
                combobox.Items.Add(item);
            }
            combobox.Items.Add("攻击");
       
            combobox.SelectedIndex = 0;
            CurrentAnimatName = combobox.Items[0] as string;
            base.OnBodyPackLoaded();
        }
        public class UpperBody : NSpirit
        {
            public UpperBody()
            {
                Z = -1;
            }
            double offsety = 1;
            TimeSpan timespan = TimeSpan.FromMilliseconds(240);
            ITimer calltimer = new CallTimer();
            public override void OnUpdata(BaseObject sender, ITimer timer)
            {
                if (calltimer.LastCallTimeSpan > timespan)
                {
                    offsety = -offsety;
                    this.Position = new Point(Position.X, Position.Y + offsety);
                    calltimer.Call();
                }
                base.OnUpdata(sender, timer);
            }
        }
    }
    
}
